from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import AuthenticationForm


class LoginForm(AuthenticationForm):
    pass


class RegisterForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'password', 'first_name', 'last_name']
        widgets = {
            'username': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '用户名'}),
            'email': forms.EmailInput(attrs={'class': 'form-control', 'placeholder': '邮箱'}),
            'password': forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': '密码'}),
            'first_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '名'}),
            'last_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '姓'})
        }
    def save(self, commit=True):
        user = super(RegisterForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password"])
        if commit:
            user.save()
        return user
